<template>
  <div class="login">
    <div>
      <h1>Edu boss管理系统</h1>
      <el-card class="card">
        登录
        <el-form :model="loginForm" status-icon ref="formRef">
          <el-form-item label="手机号" prop="phone" required>
            <el-input v-model="loginForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" required>
            <el-input
              v-model="loginForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              class="btn"
              type="primary"
              @click="submitForm()"
              v-loading="btnLoding"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { reactive, defineComponent, ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import { FETCH_URLS } from "@/config";
type DataSourceType = {
  phone: string;
  password: string;
};
export default defineComponent({
  setup() {
    const formRef = ref();
    const store = useStore();
    const router = useRouter();
    const btnLoding = ref<boolean>(false);
    const loginForm = reactive<DataSourceType>({
      phone: "15510792995",
      password: "111111",
    });

    const submitForm = () => {
      formRef.value.validate((valid: boolean) => {
        if (valid) {
          getLogin();
        } else {
          return false;
        }
      });
    };
    const getLogin = async () => {
      btnLoding.value = true;
      const params = {
        phone: loginForm.phone,
        password: loginForm.password,
      };
      const res = await request.post(FETCH_URLS.SYSTEM_LOGIN, params);
      if (res.data.state === 1) {
        store.commit("setUser", res.data.content);
        btnLoding.value = false;
        ElMessage.success("登录成功");
        router.push(
          (router.currentRoute.value.query.redirect as string) || "/"
        );
      }
    };

    return { loginForm, getLogin, submitForm, formRef, btnLoding };
  },
});
</script>
<style lang="scss" scoped>
.login {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .card {
    width: 400px;
    .btn {
      width: 100%;
    }
  }
}
</style>
